<template>
    <exPubTable ref="tableel" :cfg.sync="cfg" :exData="list" @getDataList="queryInfo" :diffHeight="30"  :defPageSize="100" :show-summary="true"></exPubTable>
</template>


<script>

export default {
    data() {
        var that =this;
        return {
            totalMoney: 0,
            lastPriceMap:{},
            list:[],
            cfg:{
                showIndex: true,
                columns: [
                    {prop:"",label:"数据合计(查询全部)",align:"center",
                        columns:[
                            {prop:"day",label:"日期",width:150,sortable:true},
                            {prop:"code",label:"代码",width:120,sortable:true},
                            {prop:"name",label:"名称",width:180,sortable:true,sumType:"add",formatter(row,col,val,idx,isSum){
                                if(isSum){
                                    var avg = that.avgFormat(row.fixedMoney,that.list.length)
                                    return `天数: ${that.list.length}   平均值: ${avg}`;
                                }
                                return val;
                            }},
                            {prop:"amount",label:"持仓变化",width:150,sortable:true,sumType:"add",align:"right",getColor(s){return that.getRateColor(s.row.amount)}},
                            {prop:"money",label:"金额变化",width:150,sortable:true,sumType:"add",formatter:this.fixed2,getColor(s){return that.getRateColor(s.row.money)}},
                            {prop:"curprice",label:"最新价格",width:150,formatter:this.fixed2},
                            {prop:"diffMoney",label:"最新市值(变化量)",width:150,sumType:"add",tips:"持仓变化 * 最新价格",formatter:this.bigMoneyFixFormat,getColor(s){return that.getRateColor(s.row.diffMoney)}},
                            {prop:"fixedMoney",label:"修正金额",width:150,sortable:true,sumType:"add",tips:"最新市值 - 金额变化",formatter:this.bigMoneyFixFormat,getColor(s){return that.getRateColor(s.row.fixedMoney)}},
                        ]
                    }
                ],
            },
        }
    },

    mounted() {
    },
    inject:["getQueryInfo"],

    methods: {
        reQuery(){
            if(this.$refs.tableel) this.$refs.tableel.reQuery();
        },
        getChartData(){
            if(this.list.length==0) return null;
            var map = {};
            this.list.map((item)=>{
                var day = item.day.substring(0,4)+"-"+item.day.substring(4,6)+"-"+item.day.substring(6,8);
                map[day]=item.fixedMoney
            })
            return map;
        },
        queryInfo(){
            this.lastPriceMap = {};
            var opername = "getSumByDayInfo";
            this.registerListen(this,opername,(res)=>{
                if(res.succ){
                    this.list = res.data;
                    // var qry = this.getQueryInfo();
                    // if(qry.filterZero=="1"){
                    //     this.list = this.list.filter((item)=>{return item.amount==0})
                    // }else if(qry.filterZero=="2"){
                    //     this.list = this.list.filter((item)=>{return item.amount!=0})
                    // }
                    this.list.map((item)=>{
                        item.fixedMoney = item.money
                    });
                    var tmplist = this.list.filter((item)=>{return item.amount!=0})
                    tmplist.map((item,idx)=>{
                        if(idx!=tmplist.length-1){
                            if(this.lastPriceMap[item.code] || this.lastPriceMap[item.code]===0) return;
                        }
                        this.lastPriceMap[item.code]=0;
                        this.getKLineInfo(item,idx==tmplist.length-1);
                    });
                }else{
                    this.$message.error("查询异常:"+res.err)
                }
            });
            
            var qry = this.getQueryInfo();
            var param = {type:opername,name:opername,objInfo:{}};
            param.execSql=true;
            var where = "";
            var wheres = [];
            if(qry.code && qry.code.in && qry.code.in.length>0){
                wheres.push( ` code in (${qry.code.in.join(",")}) `)
            }
            if(qry.day && qry.day.egt){
                wheres.push( " day >= " +qry.day.egt)
            }
            if(qry.day && qry.day.elt){
                wheres.push( " day <= " +qry.day.elt)
            }
            if(wheres.length>0){
                where = " where " + wheres.join(" and ");
            }

            
            var where2 = "";
            var wheres2 = [];
            if(qry.filterZero=="1"){
                wheres2.push( " amount = 0 ")
            }else if(qry.filterZero=="2"){
                wheres2.push( " amount != 0 ")
            }

            if(wheres2.length>0){
                where2 = " where " + wheres2.join(" and ");
            }
            param.sql=`
                select month,day${qry.groupByCode?",code,name":""},sum(amount) amount,sum(money) money from (
                    select month,day,code,name,sum(amount) amount,sum(money) money
                    from tradehis 
                    ${where}
                    group by day,code
                )
                ${where2}
                group by day${qry.groupByCode?",code":""}
                order by day desc${qry.groupByCode?",code desc":""}
                `;
            electron.ipcRenderer.send("doSql",param)
        },
        getKLineInfo(item,isLast){
            var code = item.code;
            if(!code) return;
            // if(code!="300898") return;
            var first = code.substring(0,1);
            if(code=="000001"){
                code= "1."+code;
            }else if(first=="0" || first=="3" || first=="1"){
                code = "0."+code;
            }else {
                code = "1."+code;
            }
            this.$axios.get(`http://push2his.eastmoney.com/api/qt/stock/kline/get?secid=${code}&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&ut=7eea3edcaed734bea9cbfc24409ed989&klt=101&fqt=1&beg=0&end=20500000`)
            .then((res) => {
                if (res.data.data) {
                    this.dataInfo=[];
                    var data = res.data.data;
                    this.name = data.name;
                    var len = data.klines.length;
                    var lastitem = data.klines[len-1];
                    var arr = lastitem.split(",");
                    var lastprice = arr[2]*1;
                    this.lastPriceMap[item.code]=lastprice;
                    if(isLast){
                        this.list.map((row,idx)=>{
                            if(row.amount!=0){
                                row.curprice = this.lastPriceMap[row.code]
                                row.diffMoney = this.lastPriceMap[row.code]*row.amount;
                                row.fixedMoney = this.lastPriceMap[row.code]*row.amount+row.money
                            }
                        });
                        this.list.push({});
                        this.list.pop();
                    }
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
    }
    
}
</script>

<style lang="less">

</style>
